﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SQLite;
using System.Data;

namespace CodeSnippetsWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void LoadDB()
        {
            
             //DataTable table= GetDataTable("DataBase/SnippetsSQLite.s3db","SELECT * FROM CODE");
             //this.DataContext = table;
             //for (int i = 0; i < 5;i++ )
             //{
             //    Console.WriteLine(App.CODE_DB.GetCodeSnippetByID(i));
             //}

          
            

           App.CODE_DB.LoadAllData();
           if (uilistview.DataContext == null)
           {
               uilistview.DataContext = App.CODE_DB.Code_Snippet_Buffer;
               LanguageList.ItemsSource = App.CODE_DB.Code_Language_Buffer;
               searchLanguage.ItemsSource = App.CODE_DB.Code_Language_Buffer;
           }
          
          

            

          //   DataTable table2 = GetDataTable("DataBase/SnippetsSQLite.s3db", "select DISTINCT  CODE_LANGUAGE from CODE");
           //  combobox.DataContext = table;
        }

        //public  DataTable GetDataTable(string dbPath,string sql)
        //{

        //    DataTable dt = new DataTable();

        //    try
        //    {

        //        SQLiteConnection cnn = new SQLiteConnection("Data Source=" + dbPath);

        //        cnn.Open();

        //        SQLiteCommand mycommand = new SQLiteCommand(cnn);

        //        mycommand.CommandText = sql;

        //        SQLiteDataReader reader = mycommand.ExecuteReader();

        //        dt.Load(reader);

        //        reader.Close();

        //        cnn.Close();

        //    }
        //    catch     (Exception ex)
        //    {


        //        MessageBox.Show(ex.Message); 

        //    }

        //    return dt;


        //}

        private void dgCodeDB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //selection changed
            if (e.AddedItems.Count!=0)
            {
                DataRowView drv = (DataRowView)e.AddedItems[0];
                object[] wtf = drv.Row.ItemArray.ToArray();
              
                if (wtf!=null)
                {
                    for (int i = 0; i < wtf.Length;i++ )
                    {
                        Console.WriteLine(wtf[i]);
                    }
                }
            }
         
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            App.CODE_DB.NewCodeSnippet();
           
            uilistview.DataContext = App.CODE_DB.Code_Snippet_Buffer;
          //  LanguageList.ItemsSource = App.CODE_DB.Code_Language_Buffer;
        }

       

        private void searchLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            searchText_KeyUp(sender, null);
        }

     

        private void searchText_KeyUp(object sender, KeyEventArgs e)
        {
               
            
                   if (searchLanguage.SelectedIndex!=-1)
                   {
                       App.CODE_DB.SearchByKeyWord(searchText.Text, searchLanguage.SelectedItem.ToString());
                   }
                   else
                   {
                       App.CODE_DB.SearchByKeyWord(searchText.Text, "");
                   }
                uilistview.DataContext = App.CODE_DB.Code_Snippet_Buffer;

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (newLanguage.Text == "Name a New Language" || newLanguage.Text=="")
            {
                newLanguage.Text = "";
                return;
            }

            if (LanguageList.Items.Contains(newLanguage.Text.ToUpper())==false) 
            {
                App.CODE_DB.InsertNewLanguage(newLanguage.Text.Trim());
              //  LanguageList.Items.Add(newLanguage.Text.ToUpper());
             //   searchLanguage.Items.Add(newLanguage.Text.ToUpper());
            } 
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDB();
        }

        private void btLoadDatabase_Click(object sender, RoutedEventArgs e)
        {
                  App.CODE_DB.SearchByKeyWord("", "");
                 
                uilistview.DataContext = App.CODE_DB.Code_Snippet_Buffer;
        }

        
    }
}
